export default	async function onekeylogin() {
	   return new Promise(async (resolve, reject)=>{
		  		try {
				const providerRes = await getOAuthProviders();
				console.log("预登录",providerRes);
				
				if(providerRes.errCode===30005){
					uni.showToast({
						title:'获取电话失败'
					},3000)
				     reject ( {code:false})
					return
				}
				const loginRes = await doOneKeyLogin();
				const { openid, access_token } = loginRes.authResult;
			
				if (!openid || !access_token) {
					throw new Error('openid 或 access_token 为空');
				}
				const cloudFunctionRes = await callCloudFunction(openid, access_token);
		
				if (cloudFunctionRes.result.state) {
					const phoneNumber = cloudFunctionRes.result.result.phoneNumber;
					uni.setStorage({
						key:"userTel",
						data:phoneNumber,
									})
					resolve({code:true, userTel:phoneNumber})
				
					uni.closeAuthView();
				} else {
					reject({code:false}) ; // 状态不满足，不允许跳转
				}
			} catch (e) {
				reject({code:false})
				console.error('登录过程中出现错误:', e);
				uni.showToast({
					title:'登录失败，稍后再试',
					icon:'fail'
				})
				return
				
			
			} 
	   })
	

		}
function		getOAuthProviders() {
			return new Promise((resolve, reject) => {
				uni.getProvider({
					service: 'oauth',
					success: (res) => {
						console.log("预登录信息----",res);
						resolve(res)
						
					},
					fail: (err) => reject(err)
				});
			});
		}
function		doOneKeyLogin() {
			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'univerify',
					univerifyStyle: {
						fullScreen: true,
						},
					success: (res) => resolve(res),
					fail: (err) =>{reject(err)
					console.log('一键登录失败',err)} 
				});
			});
		}
	function	callCloudFunction(openid, access_token) {
			return uniCloud.callFunction({
				name: 'oneKeyLogin',
				data: {
					openid,
					access_token
				}
			});
		}